package com.example.clxy.service.impl;

import com.example.clxy.dao.SpecialtyMapper;
import com.example.clxy.entity.Specialty;
import com.example.clxy.result.PageResult;
import com.example.clxy.result.WebRestResult;
import com.example.clxy.result.business.PageResult.SpecialtyPageResult;
import com.example.clxy.service.SpecialtyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author Hsieh
 * @title: SpecialtyServiceImpl
 * @projectName clxy
 * @description:
 * @date 2020/8/139:57
 */
@Service
public class SpecialtyServiceImpl implements SpecialtyService {
    @Autowired
    SpecialtyMapper specialtyMapper;

    @Override
    public WebRestResult addSpecialty(Specialty specialty) {
        WebRestResult result = new WebRestResult();
        specialty.setCreatedTime(new Date());
        specialty.setUpdatedTime(new Date());
        specialty.setFlag("0");
        if (specialtyMapper.insert(specialty) == 1) {
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("添加成功！");
        } else {
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("添加失败！");
        }
        return result;

    }

    @Override
    public WebRestResult deleteSpecialty(String specialtyid) {

        WebRestResult result = new WebRestResult();
        if (specialtyMapper.deleteByid(specialtyid) == 1) {
            specialtyMapper.SpecialtyUnbindWlclass(specialtyid);
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("删除成功！");
        } else {
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("删除失败！");
        }
        return result;

    }

    @Override
    public WebRestResult updateSpecialty(Specialty specialty) {
        WebRestResult result = new WebRestResult();
        specialty.setUpdatedTime(new Date());
        if (specialtyMapper.updateByPrimaryKeySelective(specialty) == 1) {
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("修改成功！");
        } else {
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("修改失败！");
        }
        return result;
    }

    @Override
    public PageResult<SpecialtyPageResult> getSpecialtybyname(String specialtyname, int page, int pagesize) {
        PageResult<SpecialtyPageResult> result = new PageResult<>();
        int begin = (page - 1) * pagesize;
        int end = pagesize;
        List<SpecialtyPageResult> list = specialtyMapper.getSpecialtybyname(specialtyname, begin, end, pagesize);
        if (list.size() > 0) {
            result.setTotal(list.get(0).getCnt());
            result.setPageCount(list.get(0).getPage());
            result.setRows(list);
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("获取列表成功！");
        } else {
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("获取列表失败！");
        }
        return result;
    }

    @Override
    public PageResult<SpecialtyPageResult> getSpecialtybyid(String specialtyid, int page, int pagesize) {
        PageResult<SpecialtyPageResult> result = new PageResult<>();
        int begin = (page - 1) * pagesize;
        int end = pagesize;
        List<SpecialtyPageResult> list = specialtyMapper.getSpecialtybyid(specialtyid, begin, end, pagesize);
        if (list.size() > 0) {
            result.setTotal(list.get(0).getCnt());
            result.setPageCount(list.get(0).getPage());
            result.setRows(list);
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("获取列表成功！");
        } else {
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("获取列表失败！");
        }
        return result;
    }

    @Override
    public WebRestResult SpecialtybindWlclass(String specialtyid,String wlclassid) {
        WebRestResult result = new WebRestResult();
        if (specialtyMapper.SpecialtybindWlclass(specialtyid,wlclassid) == 1) {
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("绑定成功！");
        } else {
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("绑定失败！");
        }
        return result;
    }
}
